package com.cqrt.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cqrt.dto.PlanDeliveryCompleteInfoDTO;
import com.cqrt.entity.RTPreConstraintDO;
import com.cqrt.service.ProjectLinkService;
import com.github.pagehelper.PageInfo;
import com.hustcad.plm.pdm.container.model.vo.search.ProjectEntityVO;
import com.hustcad.plm.pdm.core.handle.exception.SystemErrorCodeEnum;
import com.hustcad.plm.pdm.core.response.TyppmResponseResult;
import com.hustcad.plm.rpm.model.dto.issue.QueryIssueDTO;
import com.hustcad.plm.rpm.model.entity.plan.TyppmPlan;
import com.hustcad.plm.rpm.service.plan.TyppmPlanActiveService;
import com.hustcad.plm.rpm.service.plan.TyppmPlanService;
import com.ty.basic.constant.TableTypeConstrant;
import com.ty.ppm.model.constant.ApiPathConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import oracle.ucp.proxy.annotation.Post;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping(ApiPathConstant.PARENT_VERSION_ONE_PATH+ "link")
@Api(tags = "项目关联")
public class ProjectLinkController {

    @Resource
    private ProjectLinkService projectLinkService;
    @Resource
    private TyppmPlanActiveService planActiveService;


    @GetMapping("/searchAllProject")
    @ApiOperation(value = "查询所有项目", produces = "application/json; charset=utf-8")
    public TyppmResponseResult<JSONObject> searchAllProject(@Param ("keyword") String keyword,@Param("except")String except,
                                                            @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "20") int pageSize){
        PageInfo<ProjectEntityVO> pageInfo = projectLinkService.searchAllProject(keyword,except, page, pageSize);
        JSONObject json = (JSONObject) JSON.toJSON(pageInfo);
        JSONArray list = (JSONArray)json.get("list");
        for(int i = 0; Objects.nonNull(list) && i < list.size(); ++i) {
            JSONObject obj = (JSONObject)list.get(i);
                obj.put("oid", obj.get("oid").toString());
        }

        return TyppmResponseResult.success(json);
    }

    @GetMapping("/getProjectPlanByProjectOid")
    @ApiOperation(value = "根据项目id查询计划", produces = "application/json; charset=utf-8")
    public TyppmResponseResult<JSONObject> getProjectPlanByProjectOid(@Param("projectOid") String projectOid){
        JSONObject jsonObject = projectLinkService.getProjectPlanByProjectOid(projectOid);

        return TyppmResponseResult.success(jsonObject);
    }

    @PostMapping("/createProjectPlanLink")
    @ApiOperation(value = "创建关联关系", produces = "application/json; charset=utf-8")
    public TyppmResponseResult createProjectPlanLink(JSONObject params){
        this.projectLinkService.createProjectPlanLink(params);
        return TyppmResponseResult.success();
    }

    @GetMapping("/selectProjectPlanLinkByProjectId")
    @ApiOperation(value = "查询关联关系", produces = "application/json; charset=utf-8")
    public TyppmResponseResult<PageInfo<RTPreConstraintDO>> selectProjectPlanLinkByProjectId(String projectOid, @RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "20") int pageSize){
        PageInfo<RTPreConstraintDO> data = this.projectLinkService.selectProjectPlanLinkByProjectId(projectOid, page, pageSize);
        return TyppmResponseResult.success(data);
    }

    @PostMapping("queryChangeIssueListByParamsFromProject")
    @ApiOperation(
            value = "查询项目下的问题清单",
            produces = "application/json; charset=utf-8"
    )
    public TyppmResponseResult<JSONObject> queryChangeIssueListByParamsFromProject(@RequestBody @Valid QueryIssueDTO queryIssueDTO) {
        if (Objects.isNull(queryIssueDTO)) {
            throw SystemErrorCodeEnum.ARGUMENT_NONE.message(new Object[0]);
        } else {
            JSONObject list = this.projectLinkService.queryChangeIssueListByParamsFromProject(queryIssueDTO.getBaseVO(), queryIssueDTO.getJsonParams());
            return TyppmResponseResult.success(list);
        }
    }


    @GetMapping("getPlanDeliverablesCompleteStatus")
    @ApiOperation(
            value = "查询项目计划交付物齐套状态",
            produces = "application/json; charset=utf-8"
    )
    public TyppmResponseResult<List<PlanDeliveryCompleteInfoDTO>> getPlanDeliverablesCompleteStatus(@Param("projectOid") String projectOid) {
        if (Objects.isNull(projectOid)) {
            throw SystemErrorCodeEnum.ARGUMENT_NONE.message();
        } else {
            List<PlanDeliveryCompleteInfoDTO> list = this.projectLinkService.getPlanDeliveryCompleteStatus(projectOid);
            return TyppmResponseResult.success(list);
        }
    }

    @GetMapping("getProjectPlanCompareResult")
    @ApiOperation(
            value = "项目计划对比",
            produces = "application/json; charset=utf-8"
    )
    public TyppmResponseResult<JSONObject> getProjectPlanCompareResult(@Param("projectOid") String projectOid) {
        if (Objects.isNull(projectOid)) {
            throw SystemErrorCodeEnum.ARGUMENT_NONE.message();
        } else {
            JSONObject data = this.projectLinkService.getProjectPlanCompareResult(projectOid);
            return TyppmResponseResult.success(data);
        }
    }

    @GetMapping("updateProjectPlanState")
    @ApiOperation(
            value = "展示关键任务延期，阶段任务预延期，阶段任务延期，项目预延期的效果",
            produces = "application/json; charset=utf-8"
    )
    public TyppmResponseResult updateProjectPlanState() {
        this.projectLinkService.updateCriticalTaskState();
        return TyppmResponseResult.success();
    }
}
